//201902104030_何琛选
package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.DepartmentDao;
import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.School;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;
import util.bysjException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class DepartmentService {
    private static DepartmentDao departmentDao= DepartmentDao.getInstance();
    private static DepartmentService departmentService=new DepartmentService();
    private DepartmentService(){}

    public static DepartmentService getInstance(){
        return departmentService;
    }

    public Collection<Department> findAll(
            List<Condition> conditionList_json_str, Pagination pagination)
            throws SQLException {
        //获得链接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<Department> departments =
                departmentDao.findAll(connection,conditionList_json_str,pagination);
        JdbcHelper.close(connection);
        return departments;
    }

    public Integer countAll(School school) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        Integer department = departmentDao.countAll(school,connection);
        JdbcHelper.close(connection);
        return department;
    }

    public Department find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Department department = departmentDao.find(id,connection);
        JdbcHelper.close(connection);
        return department;
    }

    public boolean update(Department department) throws SQLException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = departmentDao.update(department,connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean add(Department department) throws SQLException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = departmentDao.add(department,connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean delete(Integer id) throws SQLException,bysjException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = this.delete(this.find(id));
        JdbcHelper.close(connection);
        return result;
    }

    public boolean delete(Department department) throws SQLException,bysjException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        //获得所有下一级单位（DepartmentController）
        int countTeacherOfDepartment = CommonDao.getInstance().
                countWhereForeignKey(connection,"teacher","department_id",department.getId());
        //若没有二级单位，则能够删除
        if(countTeacherOfDepartment==0){
            result = departmentDao.delete(department,connection);
        }else {
            throw new bysjException(department.getDescription()+"有下属系，不能删除");
        }
        JdbcHelper.close(connection);
        return result;
    }
}

